package top.dark.product.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.dark.common.utils.PageUtils;
import top.dark.common.utils.Query;
import top.dark.product.dao.SkuInfoDao;
import top.dark.product.entity.SkuImagesEntity;
import top.dark.product.entity.SkuInfoEntity;
import top.dark.product.entity.SpuInfoDescEntity;
import top.dark.product.service.AttrGroupService;
import top.dark.product.service.SkuImagesService;
import top.dark.product.service.SkuInfoService;
import top.dark.product.service.SpuInfoDescService;
import top.dark.product.vo.SkuItemSaleAttrVo;
import top.dark.product.vo.SkuItemVo;
import top.dark.product.vo.SpuItemAttrGroupVo;

@Slf4j
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private ExecutorService executorService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatelogId(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String catelogId = (String) params.get("catelogId");
        if (StringUtils.isNotEmpty(catelogId) && !catelogId.equals("0")) {
            queryWrapper.eq("catalog_id", catelogId);
        }
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotEmpty(brandId) && !brandId.equals("0")) {
            queryWrapper.eq("brand_id", brandId);
        }
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            queryWrapper.and(item->{
                item.eq("sku_id", key).or().like("sku_name", key).or().like("sku_title", key);
            });
        }
        String min = (String) params.get("min");
        if (StringUtils.isNotEmpty(min) && new BigDecimal(min).compareTo(BigDecimal.ZERO) == 1) {
            queryWrapper.gt("price", new BigDecimal(min));
        }
        String max = (String) params.get("max");
        if (StringUtils.isNotEmpty(max) && new BigDecimal(max).compareTo(BigDecimal.ZERO) == 1) {
            queryWrapper.lt("price", new BigDecimal(max));
        }
        IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public SkuItemVo itemInfo(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
        // 1、sku基本信息
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = this.getById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
            return skuInfo;
        }, executorService);

        // 2、sku的图片信息
        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> images = skuImagesService.images(skuId);
            skuItemVo.setImages(images);
        }, executorService);

        // 3、spu的介绍图信息
        CompletableFuture<Void> descFuture = skuInfoFuture.thenAcceptAsync(u -> {
            SpuInfoDescEntity descInfo = spuInfoDescService.getById(u.getSpuId());
            skuItemVo.setSpuInfoDesc(descInfo);
        }, executorService);

        // 4、spu下分组以及分组下基本属性信息
        CompletableFuture<Void> groupAttrFuture = skuInfoFuture.thenAcceptAsync(u -> {
            List<SpuItemAttrGroupVo> groups = attrGroupService
                    .getGroupAndAttrBySpuId(u.getSpuId(), u.getCatalogId());
            skuItemVo.setGroupAttrs(groups);
        }, executorService);

        // 5、销售属性信息
        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync(u -> {
            List<SkuItemSaleAttrVo> saleAttrVos = baseMapper.skuItemSaleAttr(u.getSpuId());
            skuItemVo.setSaleAttrs(saleAttrVos);
        }, executorService);

        CompletableFuture.allOf(imagesFuture, descFuture, groupAttrFuture, saleAttrFuture).get();

        log.info("页面需要的数据：{}", JSON.toJSONString(skuItemVo));
        return skuItemVo;
    }

}